home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13454 < prev    next >
Encoding:
Text File  |  1996-08-05  |  6.0 KB  |  158 lines

  1. Newsgroups: comp.object,comp.lang.eiffel,comp.lang.c++,comp.lang.beta,comp.lang.java,comp.lang.sather
  2. Path: lego.wes.mot.com!mothost!schbbs!news
  3. From: shang@corp.mot.com (David L. Shang)
  4. Subject: Re: What Should An Exception Handling Do? -- Clarification of rules
  5. Reply-To: shang@corp.mot.com
  6. Organization: MOTOROLA 
  7. Date: Mon, 25 Mar 1996 16:07:02 GMT
  8. Message-ID: <1996Mar25.160702.14229@schbbs.mot.com>
  9. References: <Doq3sv.MzA@research.att.com>
  10. Sender: news@schbbs.mot.com (SCHBBS News Account)
  11. Nntp-Posting-Host: 129.188.128.126
  12.  
  13. comp.object,comp.lang.eiffel,comp.lang.c++,comp.lang.beta,comp.lang.java,comp.l 
  14. ang.sather
  15.  
  16.  
  17. In article <Doq3sv.MzA@research.att.com> bs@research.att.com (Bjarne Stroustrup  
  18. <9758-26353> 0112760) writes:
  19. > billf@jovial.com (Bill Foote) writes
  20. >  > > quote explaining why C++ (and Java) uses the termination model
  21. >  > > of exception handling
  22. >  > 
  23. >  > A cynic might conclude something more along these lines:  "It would be 
  24. >  > hard to implement resumable exceptions in C++, so they decided to punt."
  25. > and that "cynic" would be wrong, guilty of not having done his homework,
  26. > and guilty of making unkind conjectures without basis in facts.
  27. >
  28.  
  29. But I don't see what's wrong with the cynic. It is true that to
  30. implement resumable exceptions in C++ is very hard. It is also
  31. true that resumable exception is useful, see below.
  32.   
  33. > Here, I'd like to quote a key section:
  34. >     Then, at the Palo Alto meeting in November 1991, we heard a
  35. >     brilliant summary of the arguments for termination semantics
  36. >     backed with both personal experience and data from Jim Mitchell
  37. >     (from Sun, formerly from Xerox PARC). Jim had used exception
  38. >     handling in half a dozen languages over a period of 20 years
  39. >     and was an early proponent of resumption semantics as one of
  40. >     the main designers and implementers of Xerox's Cedar/Mesa system.
  41. >     His message was
  42. >         ``termination is preferred over resumption; this is
  43. >         not a matter of opinion but a matter of years of
  44. >         experience. Resumption is seductive, but not valid.''
  45. >     He backed this statement with experience from several operating
  46. >     systems. The key example was Cedar/Mesa: It was written by people
  47. >     who liked and used resumption, but after ten years of use, there
  48. >     was only one use of resumption left in the half million line
  49. >     system -- and that was a context inquiry. Because resumption
  50. >     wasn't actually necessary for such a context inquiry, they removed
  51. >     it and found a significant speed increase in that part of the
  52. >     system. In each and every case where resumption had been used
  53. >     it had -- over the ten years -- become a problem and a more
  54. >     appropriate design had replaced it. Basically, every use of
  55. >     resumption had represented a failure to keep separate levels
  56. >     of abstraction disjoint
  57. >     Mary Fontana presented similar data from the TI Explorer system
  58. >     where resumption was found to be used for debugging only, Aron
  59. >      Insinga presented evidence of the very limited and nonessential
  60. >     use of resumption in DEC's VMS, and Kim Knuttilla related exactly
  61. >     the same story as Jim Mitchell for two large and long-lived
  62. >     projects inside IBM. To this we added a strong opinion in favor
  63. >      of termination based on experience at L.M.Ericsson relayed to
  64. >     us by Dag Bruck.
  65. >     Thus, the C++ committee endorsed termination semantics.
  66.  
  67. The key point is: WHAT IS AN EXCEPTION?
  68.  
  69. The definition:
  70.  
  71. >    "Exception handling is intended to allow code that has encountered 
  72. >a condition it cannot cope with to return to some other code that 
  73. >directly or indirectly invoked it. There is no way for an exception 
  74. >handler to request the thread of control to resume from the throw point. 
  75. >In other words, "throw" implements the termination model of exception 
  76. >handling."     -ARM, Ellis & Stroustrup, page 354
  77. >
  78.  
  79. narrows exceptions to error conditions only. If it is this case
  80. termination would be sufficient, and resumptions would be useless.
  81.  
  82. But an exception is not necessarily an error. Sometimes it is an
  83. condition that requires some extraordinary computation, a condition
  84. that is not supposed for a regular case, for example, to open a
  85. configuration file in an application directory and the file is not
  86. found. This condition requires a further processing, e.g. to look up
  87. the file in system directory. This is a quite common case in every
  88. system design. More examples:
  89.  
  90.       * the font does not exist and a query to the user is required to
  91.     get the substituting font;
  92.       * the input is in the wrong type and an input retry is required;
  93.       * the file is not associated with a default handler, would you
  94.     like to associate one and let me try to re-open the file?
  95.       * the format is not understood, would you suggest me a
  96.     converter?
  97.       * the embedded object is re-located, would give me the directory
  98.     to the new place?
  99.  
  100. These conditions are not errors but only require some extraordinary work.
  101. Similar examples were also given by Bill Foote in his previous post.
  102. It is not hard for people to figure out more examples that requires the
  103. following struture:
  104.  
  105.  result    = try do_something()
  106.       {
  107.         when condition1: some_extraordinary_work1; retry;
  108.         when condition2: some_extraordinary_work2; retry;
  109.         when condition3: some_extraordinary_work3; retry;
  110.         when condition4: return null;
  111.       }
  112.  
  113. is certainly better than:
  114.  
  115.     result = do_something();
  116.     exceptional_condition = check_error_message();
  117.     while (exceptional_condition)
  118.     {
  119.         if (exceptional_condition==condition4)
  120.         {
  121.         result = null;
  122.         break;
  123.         }
  124.         switch (exceptional_condition)
  125.         {
  126.         case condition1:
  127.             some_extraordinary_work1;
  128.             do_something();
  129.             break;
  130.         case condition2:
  131.             some_extraordinary_work2;
  132.             do_something();
  133.             break;
  134.         case condition3:
  135.             some_extraordinary_work3;
  136.             do_something();
  137.             break;
  138.         }
  139.     }
  140.  
  141. Oh, well! It takes me ten time longer to figure out the second piece of
  142. code and I am still not sure whether this code is correct or not. After
  143. a second look, yes, there are errors! "check_error_message()" should also
  144. be called in the loop to get the new exceptional condition after a retry.
  145.  
  146. David Shang
  147.  
  148.  
  149.